﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using DBUtil;
using ZXCompany.Models.Entity;
namespace ZXCompany.Models.Dao
{
    public class ZX_WholeInfoDao
    {
        private Database db = new Database();
        string sqlstr = "";

        /// <summary>
        /// 数据插入
        /// </summary>
        /// <param name="cinfo"></param>
        /// <param name="sconn"></param>
        public void Insert(ZX_WholeInfo cinfo, SqlConnection sconn)
        {
            sqlstr = @"INSERT INTO [ZX_WholeInfo]
                           ([detailInfo]
                           ,[addUserID]
                           ,[addTime])
                     VALUES
                           (@detailInfo
                           ,@addUserID
                           ,@addTime)";
            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@detailInfo",cinfo.detailInfo),
                new SqlParameter("@addUserID",cinfo.addUserID),
                new SqlParameter("@addTime",cinfo.addTime)
            };
            try
            {
                db.Udaquery(sqlstr, sconn, para);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 数据更新
        /// </summary>
        /// <param name="cinfo"></param>
        /// <param name="sconn"></param>
        public void Update(ZX_WholeInfo cinfo, SqlConnection sconn)
        {
            sqlstr = @"update [ZX_WholeInfo]
                           set [detailInfo] =@detailInfo,
                               updateTime =@updateTime
                           where id=@id";
            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@detailInfo", cinfo.detailInfo ),
                new SqlParameter("@updateTime",cinfo.updateTime),
                new SqlParameter("@id",cinfo.ID)
            };
            try
            {
                db.Udaquery(sqlstr, sconn, para);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 数据选择
        /// </summary>
        /// <param name="sconn"></param>
        /// <returns></returns>
        public ZX_WholeInfo Select(SqlConnection sconn)
        {
            ZX_WholeInfo Cinfo = new ZX_WholeInfo();
            sqlstr = @"select top 1 isnull(id,0) as id
                           ,isnull([detailInfo],'') as detailInfo
                           ,isnull([addUserID],0) as addUserID
                           ,isnull([addTime],'') as addTime
                        from  ZX_WholeInfo";
            try
            {
                DataTable dt = db.Squery(sqlstr, sconn);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    Cinfo.ID = Convert.ToInt32(dr["id"]);
                    Cinfo.detailInfo = dr["detailInfo"].ToString();
                    Cinfo.addUserID = Convert.ToInt32(dr["addUserID"]);
                    Cinfo.addTime = dr["addTime"].ToString();
                }
                else
                {
                    Cinfo.ID = 0;
                    Cinfo.detailInfo = "";
                }
                return Cinfo;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}